<?php

namespace App\Http\Controllers\admin\users;

use App\Http\Controllers\Controller;
use App\Models\users\UsersAmountRecordModel;
use Illuminate\Http\JsonResponse;
use lxs\api\ApiResponse;

class RecordController extends Controller
{
    /**
     * 获取收益记录页面数据
     */
    public function getIncomeListData(): JsonResponse
    {
        $data = request()->input();

        $query = UsersAmountRecordModel::with([
            'userInfo' => function ($query) {
                $query->select('id', 'nickname', 'avatar', 'mobile');
            },
        ])->where('deleted_at', 0);

        if (isset($data['type']) && $data['type'] != 0) {
            $query->where('type', $data['type']);
        }

        if (isset($data['credit_type']) && $data['credit_type'] != 0) {
            $query->where('credit_type', $data['credit_type']);
        }

        if (isset($data['create_at']) && $data['create_at'] != '') {
            $query->whereBetween('create_at', [$data['create_at'][0], $data['create_at'][1]]);
        }

        $lists = $query->orderBy('create_at', 'desc')->paginate($data['pageSize']);
        foreach ($lists as $item) {
            $item->type_name = UsersAmountRecordModel::getTypeName[$item->type] ?? '未知';
            $item->credit_type_name = UsersAmountRecordModel::getCreditTypeName[$item->credit_type] ?? '未知';
        }

        $consumptionTypeData = UsersAmountRecordModel::getTypeName;
        $creditTypeData = UsersAmountRecordModel::getCreditTypeName;

        return ApiResponse::apiResponseData(200, '获取收益记录页面数据成功', [
            'lists' => $lists,
            'consumptionTypeData' => $consumptionTypeData,
            'creditTypeData' => $creditTypeData,
        ]);
    }

}
